/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telsoft.cms_admin.user;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;
import telsoft.util.StringUtil;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "MPB")
@SessionScoped
public class ModifyPassBean implements Serializable {

    private String strOldPass = "";
    private String strNewPass = "";
    private String strUserName = "";
    private String strPass = "";
    private String strRNewPass = "";

    public ModifyPassBean() {
    }

    public void onUpdate() {
        
        FacesContext fc = FacesContext.getCurrentInstance();
        Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
        strUserName = (String) params.get("strUserLogin");
        strPass = (String) params.get("strPassLogin");
        if (strOldPass.equals("") || strNewPass.equals("") || strRNewPass.equals("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Các trường đánh dấu (*) không được để trống"));
        } else if (!strPass.equals(strOldPass)) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Mật khẩu cũ không đúng"));
        } 
        else if(strNewPass.length() < 5){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Mật khẩu mới phải dài ít nhất 6 ký tự"));
        }
        else if (!strNewPass.equals(strRNewPass)) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Mật khẩu mới và mật khẩu xác nhận phải trùng nhau"));

        } else {
            Connection conn = null;
            PreparedStatement stmt = null;
            try {
                String strPwEncrypted = StringUtil.encrypt(strNewPass, "SHA");
                conn = AppServer.getConnection();
                String strSQL = "Update sec_user Set password = ? "
                        + "where user_name = ? ";
                stmt = conn.prepareStatement(strSQL);
                stmt.setString(1, strPwEncrypted);
                stmt.setString(2, strUserName);
                stmt.executeUpdate();
                onReset();
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Mật khẩu đã được đổi thành công..!!"));
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                Database.closeObject(stmt);
                Database.closeObject(conn);
            }
        }
    }

    public void onReset() {
        strOldPass = "";
        strNewPass = "";
        strRNewPass = "";
        return;
    }

    public void setStrOldPass(String strOldPass) {
        this.strOldPass = strOldPass;
    }

    public String getStrOldPass() {
        return strOldPass;
    }

    public void setStrNewPass(String strNewPass) {
        this.strNewPass = strNewPass;
    }

    public String getStrNewPass() {
        return strNewPass;
    }

    public void setStrRNewPass(String strRNewPass) {
        this.strRNewPass = strRNewPass;
    }

    public String getStrRNewPass() {
        return strRNewPass;
    }
}
